Skip to content

Method: UpdateOperationsOnGetReferenceRunner(Logger, PersistenceFactory, DataAccessor)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.test.runner;
19:
20: import cz.cvut.kbss.jopa.test.*;
21: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
22: import cz.cvut.kbss.jopa.test.environment.Generators;
23: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
24: import org.junit.jupiter.api.Test;
25: import org.slf4j.Logger;
26:
27: import java.util.Collections;
28: import java.util.Set;
29: import java.util.stream.Collectors;
30: import java.util.stream.IntStream;
31:
32: import static org.junit.jupiter.api.Assertions.*;
33:
34: public abstract class UpdateOperationsOnGetReferenceRunner extends BaseRunner {
35:
36: protected UpdateOperationsOnGetReferenceRunner(Logger logger, PersistenceFactory persistenceFactory,
37: DataAccessor dataAccessor) {
38: super(logger, persistenceFactory, dataAccessor);
39: }
40:
41: @Test
42: void getReferenceResultCanBeUsedAsAttributeValueInUpdate() {
43: this.em = getEntityManager("getReferenceResultCanBeUsedAsAttributeValueInUpdate", true);
44: entityD.setOwlClassA(null);
45: persist(entityD, entityA);
46:
47: em.getTransaction().begin();
48: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
49: d.setOwlClassA(em.getReference(OWLClassA.class, entityA.getUri()));
50: em.getTransaction().commit();
51:
52: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
53: assertNotNull(result.getOwlClassA());
54: assertEquals(entityA.getUri(), result.getOwlClassA().getUri());
55: }
56:
57: @Test
58: void getReferenceResultDataAttributesCanBeAssignedNewValuesInUpdate() {
59: this.em = getEntityManager("getReferenceResultDataAttributesCanBeAssignedNewValuesInUpdate", true);
60: persist(entityM);
61:
62: em.getTransaction().begin();
63: final OWLClassM m = em.getReference(OWLClassM.class, entityM.getKey());
64: final int iVal = Generators.randomInt();
65: m.setIntAttribute(iVal);
66: assertEquals(iVal, m.getIntAttribute().intValue());
67: final long lVal = System.currentTimeMillis();
68: m.setLongAttribute(lVal);
69: final Set<Integer> intSet = IntStream.generate(Generators::randomInt).limit(5).boxed()
70: .collect(Collectors.toSet());
71: m.setIntegerSet(intSet);
72: em.getTransaction().commit();
73:
74: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
75: assertEquals(iVal, result.getIntAttribute().intValue());
76: assertEquals(lVal, result.getLongAttribute().longValue());
77: assertEquals(intSet, result.getIntegerSet());
78: // Ensure other values were not changed
79: assertEquals(entityM.getBooleanAttribute(), result.getBooleanAttribute());
80: assertEquals(entityM.getEnumAttribute(), result.getEnumAttribute());
81: assertEquals(entityM.getDateAttribute(), result.getDateAttribute());
82: assertEquals(entityM.getDoubleAttribute(), result.getDoubleAttribute());
83: }
84:
85: @Test
86: void getReferenceResultObjectAttributesCanBeAssignedNewValuesInUpdate() {
87: this.em = getEntityManager("getReferenceResultObjectAttributesCanBeAssignedNewValuesInUpdate", true);
88: entityD.setOwlClassA(null);
89: persist(entityD, entityA);
90:
91: em.getTransaction().begin();
92: final OWLClassD d = em.getReference(OWLClassD.class, entityD.getUri());
93: final OWLClassA a = em.getReference(OWLClassA.class, entityA.getUri());
94: d.setOwlClassA(a);
95: em.getTransaction().commit();
96:
97: final OWLClassD result = em.find(OWLClassD.class, entityD.getUri());
98: assertEquals(entityA.getUri(), result.getOwlClassA().getUri());
99: }
100:
101: @Test
102: void getReferenceResultPluralObjectAttributeCanBeUpdated() {
103: this.em = getEntityManager("getReferenceResultPluralObjectAttributeCanBeUpdated", true);
104: final OWLClassJ entityJ = new OWLClassJ(Generators.generateUri());
105: entityJ.setOwlClassA(Collections.singleton(entityA));
106: persist(entityJ);
107:
108: em.getTransaction().begin();
109: final OWLClassJ j = em.getReference(OWLClassJ.class, entityJ.getUri());
110: final OWLClassA newA = new OWLClassA(Generators.generateUri());
111: newA.setStringAttribute("newA");
112: j.getOwlClassA().add(newA);
113: em.persist(newA);
114: em.getTransaction().commit();
115:
116: final OWLClassJ result = findRequired(OWLClassJ.class, entityJ.getUri());
117: assertEquals(j.getOwlClassA().size(), result.getOwlClassA().size());
118: assertTrue(j.getOwlClassA().stream().anyMatch(a -> a.getUri().equals(entityA.getUri())));
119: assertTrue(j.getOwlClassA().stream().anyMatch(a -> a.getUri().equals(newA.getUri())));
120: }
121:
122: @Test
123: void getReferenceResultListAttributeCanBeUpdated() {
124: this.em = getEntityManager("getReferenceResultListAttributeCanBeUpdated", true);
125: final OWLClassK entityK = new OWLClassK();
126: entityK.setReferencedList(Collections.singletonList(entityE));
127: entityK.setSimpleList(Collections.singletonList(entityE));
128: persist(entityK, entityE);
129:
130: em.getTransaction().begin();
131: final OWLClassK upd = em.getReference(OWLClassK.class, entityK.getUri());
132: final OWLClassE newE = new OWLClassE();
133: upd.getReferencedList().add(newE);
134: upd.getSimpleList().add(newE);
135: em.persist(newE);
136: em.getTransaction().commit();
137:
138: final OWLClassK result = findRequired(OWLClassK.class, entityK.getUri());
139: assertEquals(upd.getReferencedList().size(), result.getReferencedList().size());
140: assertEquals(upd.getSimpleList().size(), result.getSimpleList().size());
141: assertTrue(result.getReferencedList().stream().anyMatch(a -> a.getUri().equals(newE.getUri())));
142: assertTrue(result.getSimpleList().stream().anyMatch(a -> a.getUri().equals(newE.getUri())));
143: }
144:
145: @Test
146: void assigningGetReferenceResultToInstanceRemovesItFromCacheToPreventSubsequentIncompleteDataRetrieval() {
147: this.em = getEntityManager(
148: "assigningGetReferenceResultToInstanceRemovesItFromCacheToPreventSubsequentIncompleteDataRetrieval",
149: true);
150: entityD.setOwlClassA(null);
151: persist(entityD, entityA);
152:
153: em.getEntityManagerFactory().getCache().evictAll();
154: em.getTransaction().begin();
155: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
156: final OWLClassA a = em.getReference(OWLClassA.class, entityA.getUri());
157: d.setOwlClassA(a);
158: em.getTransaction().commit();
159:
160: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
161: em.detach(result);
162: assertNotNull(result.getOwlClassA());
163: assertNotNull(result.getOwlClassA().getStringAttribute());
164: }
165:
166: @Test
167: void mergeWithFieldValueBeingResultOfGetReferenceRemovesItFromCacheToPreventSubsequentIncompleteDataRetrieval() {
168: this.em = getEntityManager(
169: "mergeWithFieldValueBeingResultOfGetReferenceRemovesItFromCacheToPreventSubsequentIncompleteDataRetrieval",
170: true);
171: entityD.setOwlClassA(null);
172: persist(entityD, entityA);
173:
174: em.getEntityManagerFactory().getCache().evictAll();
175: em.getTransaction().begin();
176: final OWLClassA a = em.getReference(OWLClassA.class, entityA.getUri());
177: entityD.setOwlClassA(a);
178: em.merge(entityD);
179: em.getTransaction().commit();
180:
181: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
182: em.detach(result);
183: assertNotNull(result.getOwlClassA());
184: assertNotNull(result.getOwlClassA().getStringAttribute());
185: }
186: }